{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "多层感知机的简洁实现\n",
    "'''\n",
    "import d2lzh as d2l\n",
    "from mxnet import gluon, init\n",
    "from mxnet.gluon import loss as gloss, nn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class MyInit(init.Initializer):\n",
    "    def __init__(self):\n",
    "        super(MyInit, self).__init__()\n",
    "        self._verbose = True\n",
    "    def _init_weight(self, _, arr):\n",
    "        # 初始化权重，使用out=arr后我们不需指定形状\n",
    "        print('init weight', arr.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "init weight (256, 784)\n",
      "init weight (64, 256)\n",
      "init weight (64, 64)\n",
      "init weight (128, 64)\n",
      "init weight (10, 128)\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "定义模型\n",
    "'''\n",
    "net = nn.Sequential()\n",
    "shared = nn.Dense(64, in_units=256, activation='relu')\n",
    "net.add(nn.Dense(256, in_units=784, activation='relu'),\n",
    "        shared,\n",
    "        nn.Dense(64, in_units=64 ,activation='relu'),\n",
    "        nn.Dense(128, in_units=64, activation='relu'),\n",
    "        nn.Dense(10, in_units=128))\n",
    "net.initialize(MyInit())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 1, loss 2.3031, train acc 0.099, test acc 0.100\n",
      "epoch 2, loss 2.3030, train acc 0.100, test acc 0.100\n",
      "epoch 3, loss 2.3031, train acc 0.099, test acc 0.100\n",
      "epoch 4, loss 2.3032, train acc 0.099, test acc 0.100\n",
      "epoch 5, loss 2.3031, train acc 0.099, test acc 0.100\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "训练模型\n",
    "'''\n",
    "batch_size = 256\n",
    "train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)\n",
    "\n",
    "loss = gloss.SoftmaxCrossEntropyLoss()\n",
    "trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.5})\n",
    "num_epochs = 5\n",
    "d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, None,\n",
    "              None, trainer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "sequential6_ (\n",
       "  Parameter dense22_weight (shape=(64, 784), dtype=float32)\n",
       "  Parameter dense22_bias (shape=(64,), dtype=float32)\n",
       "  Parameter dense23_weight (shape=(64, 64), dtype=float32)\n",
       "  Parameter dense23_bias (shape=(64,), dtype=float32)\n",
       "  Parameter dense24_weight (shape=(128, 64), dtype=float32)\n",
       "  Parameter dense24_bias (shape=(128,), dtype=float32)\n",
       "  Parameter dense25_weight (shape=(10, 128), dtype=float32)\n",
       "  Parameter dense25_bias (shape=(10,), dtype=float32)\n",
       ")"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.collect_params()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
